<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Member;
use App\Admin\Models\OrderCommissions;
use App\Admin\Models\Orders;
use App\Admin\Models\Wallets;
use Common\CommonAd;
use Common\ControllerAdBase;
use Common\Models\User;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class TransactionController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function homeCountAction()
    {
        $user                      = new User();
        $today                     = ToolsController::timeChange(1);
        $yesterday                 = ToolsController::timeChange(2);
        $result['today_user']      = $user->userCount($today);
        $result['yesterday_user']  = $user->userCount($yesterday);
        $order                     = new Orders();
        $result['today_order']     = $order->orderCount($today);
        $result['yesterday_order'] = $order->orderCount($yesterday);

        $result['today_commition']     = $order->orderCountCommition($today);
        $result['yesterday_commition'] = $order->orderCountCommition($yesterday);
        $this->ok($result);
    }

    /**
     * 统计之和
     */
    public function statisticscountAction()
    {
        $order  = new Orders();
        $member = new Member();
        //会员数
        $today   = ToolsController::timeChange(1);
        $lastday = ToolsController::timeChange(2);

        $toweek               = ToolsController::timeChange(3);
        $lastweek['end_at']   = $toweek['start_at'];
        $lastweek['start_at'] = date('Y-m-d H:i:s', strtotime('-6 day', strtotime($toweek['start_at'])));

        $data['members']['membersCount']      = $member->countNumber();
        $data['members']['thedaybefore']      = $member->userCompare($today, $lastday);//较前一日
        $data['members']['thedaybefore_flag'] = $data['members']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['members']['lastweek']          = $member->userCompare($toweek, $lastweek);//较上周
        $data['members']['lastweek_flag']     = $data['members']['lastweek'] > 0 ? 1 : 0;//上周同期

        //订单数
        $data['order']['orderCount']        = $order->orderCount();
        $data['order']['thedaybefore']      = $order->orderCompare($today, $lastday);//较前一日
        $data['order']['thedaybefore_flag'] = $data['order']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['order']['lastweek']          = $order->orderCompare($toweek, $lastweek);//较上周
        $data['order']['lastweek_flag']     = $data['order']['lastweek'] > 0 ? 1 : 0;;//上周同期

        //本月预估
        $month                                  = ToolsController::timeChange(8);
        $data['thismonth']['thismonthCount']    = $order->orderCountCommition($month);
        $data['thismonth']['thedaybefore']      = $order->orderCompareCommition($today, $lastday);
        $data['thismonth']['thedaybefore_flag'] = $data['thismonth']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['thismonth']['thismonth']         = $order->orderCompareCommition($toweek, $lastweek);
        $data['thismonth']['lastweek_flag']     = $data['thismonth']['thismonth'] > 0 ? 1 : 0;;//上周同期

        //上月预估

        $lastmonth                       = ToolsController::timeChange(9);
        $data['upmonth']['upmonthCount'] = $order->orderCountCommition($lastmonth);
        $lastlastmonth['start_at']       = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 2, 1, date("Y")));

        $lastlastmonth['end_at']              = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m") - 1, 0, date("Y")));
        $data['upmonth']['thedaybefore']      = $order->orderCompareCommition($lastmonth, $lastlastmonth);
        $data['upmonth']['thedaybefore_flag'] = $data['upmonth']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降

        //本年预估
        $lastyear                              = ToolsController::timeChange(12);
        $data['thisyear']['thisyearCount']     = $order->orderCountCommition($lastyear);
        $lastlastyear                          = ToolsController::timeChange(13);
        $data['thisyear']['thedaybefore']      = $order->orderCompareCommition($lastyear, $lastlastyear);//去年
        $data['thisyear']['thedaybefore_flag'] = $data['thisyear']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降


        //本月自营预估
        $data['selfdobusiness']['selfdobusinessCount'] = '10000';
        $data['selfdobusiness']['thedaybefore']        = '0.17%';
        $data['selfdobusiness']['thedaybefore_flag']   = 1;//1上升0下降
        $data['selfdobusiness']['lastweek']            = '0.14%';
        $data['selfdobusiness']['lastweek_flag']       = 0;//上周同期

        $orderCommission = new Wallets();
        $data['countData']['commission']     = $orderCommission->get_amount();
        $data['countData']['gold_count']     = 0;
        $data['countData']['integral_count'] = 0;
        $data['countData']['wit_gold']       = 0;
        $exc_gold                            = 0;
        $data['countData']['exc_gold']       = $exc_gold ? $exc_gold : 0;
        $this->ok($data);
    }

    /**
     * 新统计
     */
    public function newCountAction()
    {

        //未提现佣金
        $wall                            = new Wallets();
        $result['UncollectedCommission'] = $wall->amountAll();
        //累计产生佣金
        $OrderCommissions                = new OrderCommissions();
        $result['AccumulatedCommission'] = $OrderCommissions->commissionAll();
        //冻结中金币
        $result['FrozenGoldCoin'] = 0;
        //已提现金币
        $result['WithdrawnGoldCoins'] = 0;
        //累计产生金币
        $result['AccumulatedGoldCoins'] = 0;


        //未兑换金币
        $result['GoldCoinNotExchanged'] = 0;
        //本月预估收入
        $order                                 = new Orders();
        $month                                 = CommonAd::timeChange(8);
        $result['EstimatedRevenueForTheMonth'] = $order->moneyAll($month);
        //上月预估收入
        $month                               = CommonAd::timeChange(9);
        $result['EstimatedRevenueLastMonth'] = $order->moneyAll($month);

        //累计产生积分
        $result['AccumulatedPoints'] = 0;
        //已兑换积分
        $PointsEedeemed           = 0;
        $result['PointsEedeemed'] = 0;
        //冻结积分
        $result['FreezingIntegral'] = $order->frozenAll();
        //未兑换积分
        $result['PointsNotRedeemed'] = 0;
        $this->ok($result);
    }

    public function orderCountAction()
    {
        $time = CommonAd::timeChange(14);
        if (!isset($this->params['start_at']) || empty($this->params['start_at'])) {
            $this->params['start_at'] = $time['start_at'];
        }
        if (!isset($this->params['end_at']) || empty($this->params['end_at'])) {
            $this->params['end_at'] = $time['end_at'];
        }
        $order         = new Orders();
        $data          = $order->orderCountInfo($this->params);
        $result['day'] = $this->getDateFromRange($this->params['start_at'], $this->params['end_at']);
        foreach ($result['day'] as $val) {
            if (isset($data[$val])) {
                $result['data'][] = $data[$val]['count'];
            } else {
                $result['data'][] = 0;
            }
        }

        $time                                    = CommonAd::timeChange(1);
        $result['order_all'] = $order->orderDayCount_all();
        $result['info']['today_order_count']     = $order->orderDayCount($time);
        $time                                    = CommonAd::timeChange(2);
        $result['info']['yesterday_order_count'] = $order->orderDayCount($time);
        $result['info']['today_ratio']           = CommonAd::number_randtwo($this->compareAdd($result['info']['today_order_count'], $result['info']['yesterday_order_count']));

        $time                                     = CommonAd::timeChange(14);
        $result['info']['week_order_count']       = $order->orderDayCount($time);
        $time['start_at']                         = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("w") + 1 - 7, date("Y")));
        $time['end_at']                           = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("d") - date("w") + 7 - 7, date("Y")));
        $result['info']['yesterweek_order_count'] = $order->orderDayCount($time);
        $result['info']['week_ratio']             = CommonAd::number_randtwo($this->compareAdd($result['info']['week_order_count'], $result['info']['yesterweek_order_count']));

        $time                                      = CommonAd::timeChange(8);
        $result['info']['month_order_count']       = $order->orderDayCount($time);
        $time                                      = CommonAd::timeChange(9);
        $result['info']['yestermonth_order_count'] = $order->orderDayCount($time);
        $result['info']['month_ratio']             = CommonAd::number_randtwo($this->compareAdd($result['info']['month_order_count'], $result['info']['yestermonth_order_count']));

        $this->ok($result);
    }

    public function compareAdd($a, $b)
    {

        $a = (int)$a;
        $b = (int)$b;
        if ($a == $b) {
            return 0;
        } elseif ($b == 0 && $a > 0) {
            return 100;
        } elseif ($a == 0 && $b > 0) {
            return -100;
        } elseif ($a > $b) {
            $c = CommonAd::number_randtwo(($a - $b) / $b, 5);
            $c = $c * 100;
            return $c;
        } elseif ($a < $b) {
            $c = CommonAd::number_randtwo(($a - $b) / $b, 5);
            $c = $c * 100;
            return $c;
        } else {
            return "系统错误";
        }
    }

    public function getDateFromRange($startdate, $enddate)
    {
        $stimestamp = strtotime($startdate);
        $etimestamp = strtotime($enddate);
        // 计算日期段内有多少天
        $days = ($etimestamp - $stimestamp) / 86400;
        // 保存每天日期
        $date = array();
        for ($i = 0; $i < $days; $i++) {
            $date[] = date('Y-m-d', $stimestamp + (86400 * $i));
        }
        return $date;
    }

    /**
     * 查找最近七天的计入
     */
    public function statisticsAction()
    {
        $thisD    = date('d');
        $thisM    = date('Y-m');
        $weekDate = [];
        for ($i = $thisD; $i > $thisD - 7; $i--) {
            if (strlen($i) == 1) {
                $weekDate[] = $thisM . '-0' . $i;
            } else {
                $weekDate[] = $thisM . '-' . $i;
            }
        }
        sort($weekDate);
        $week                = ToolsController::timeChange(3);
        $user                = new User();
        $weekBuyed0          = $user->userStatistics($week, 0);
        $weekBuyed1          = $user->userStatistics($week, 1);
        $membersActualData   = [];
        $membersExpectedData = [];

        $order             = new Orders();
        $orderActualData   = $order->orderStatistics($week, '1,2,4,6');
        $orderExpectedData = $order->orderStatistics($week, '4,6');

        $orderActualDataNew   = [];
        $orderExpectedDataNew = [];
        foreach ($weekDate as $val) {
            if (isset($weekBuyed1[$val])) {
                $membersExpectedData[] = $weekBuyed1[$val];
            } else {
                $membersExpectedData[] = 0;
            }
            if (isset($weekBuyed0[$val])) {
                $membersActualData[] = $weekBuyed0[$val];
            } else {
                $membersActualData[] = 0;
            }

            if (isset($orderActualData[$val])) {
                $orderActualDataNew[] = $orderActualData[$val];
            } else {
                $orderActualDataNew[] = 0;
            }
            if (isset($orderExpectedData[$val])) {
                $orderExpectedDataNew[] = $orderExpectedData[$val];
            } else {
                $orderExpectedDataNew[] = 0;
            }
        }
        $data['members']['datetime']             = $weekDate;//7天会员
        $data['members']['data']['actualData']   = $membersActualData;//潜在会员
        $data['members']['data']['expectedData'] = $membersExpectedData;//有效会员


        $data['order']['datetime']             = $weekDate;//7天订单
        $data['order']['data']['actualData']   = $orderActualDataNew;//预估收入1，2，4,6
        $data['order']['data']['expectedData'] = $orderExpectedDataNew;//结算收入4 6

        $thismonthWeek         = ToolsController::timeChange(8);
        $thismonthActualData   = $order->orderStatistics($thismonthWeek, '1,2,4,6');
        $thismonthExpectedData = $order->orderStatistics($thismonthWeek, '4,6');
        $thismonthDate         = [];
        for ($i = $thisD; $i > 0; $i--) {
            if (strlen($i) == 1) {
                $thismonthDate[] = $thisM . '-0' . $i;
            } else {
                $thismonthDate[] = $thisM . '-' . $i;
            }
        }
        sort($thismonthDate);
        $thismonthActualDataNew   = [];
        $thismonthExpectedDataNew = [];
        foreach ($thismonthDate as $val) {
            if (isset($thismonthActualData[$val])) {
                $thismonthActualDataNew[] = $thismonthActualData[$val];
            } else {
                $thismonthActualDataNew[] = 0;
            }
            if (isset($thismonthExpectedData[$val])) {
                $thismonthExpectedDataNew[] = $thismonthExpectedData[$val];
            } else {
                $thismonthExpectedDataNew[] = 0;
            }
        }

        $data['thismonth']['datetime']             = $thismonthDate;//本月收入
        $data['thismonth']['data']['actualData']   = $thismonthActualDataNew;//预估收入
        $data['thismonth']['data']['expectedData'] = $thismonthExpectedDataNew;//结算收入


        $upmonthDay = date('t', strtotime('-1 month'));
        $upmonthM   = date('Y-m', strtotime('-1 month'));
        for ($i = $upmonthDay; $i > 0; $i--) {
            if (strlen($i) == 1) {
                $upmonthDate[] = $upmonthM . '-0' . $i;
            } else {
                $upmonthDate[] = $upmonthM . '-' . $i;
            }
        }
        sort($upmonthDate);
        $upmonthdatetime     = ToolsController::timeChange(9);
        $upmonthactualData   = $order->orderStatistics($upmonthdatetime, '1,2,4,6');
        $upmonthexpectedData = $order->orderStatistics($upmonthdatetime, '4,6');

        $upmonthactualDataNew   = [];
        $upmonthexpectedDataNew = [];
        foreach ($upmonthDate as $val) {
            if (isset($upmonthactualData[$val])) {
                $upmonthactualDataNew[] = $upmonthactualData[$val];
            } else {
                $upmonthactualDataNew[] = 0;
            }
            if (isset($upmonthexpectedData[$val])) {
                $upmonthexpectedDataNew[] = $upmonthexpectedData[$val];
            } else {
                $upmonthexpectedDataNew[] = 0;
            }
        }

        $data['upmonth']['datetime']             = $upmonthDate;//上月收入
        $data['upmonth']['data']['actualData']   = $upmonthactualDataNew;//预估收入
        $data['upmonth']['data']['expectedData'] = $upmonthexpectedDataNew;//结算收入

        $thisMonth = date('m');
        $thisyear  = date('Y');
        for ($i = $thisMonth; $i > 0; $i--) {
            if (strlen($i) == 1) {
                $thisMonthhDate[] = $thisyear . '-0' . $i;
            } else {
                $thisMonthhDate[] = $thisyear . '-' . $i;
            }
        }
        sort($thisMonthhDate);
        $thisYaer             = ToolsController::timeChange(12);
        $thisYaer['end_at']   = date('Y-m-d H:i:s');
        $thisyearactualData   = $order->orderStatisticsMonth($thisYaer, '1,2,4,6');
        $thisyearexpectedData = $order->orderStatisticsMonth($thisYaer, '4,6');

        $thisyearactualDataNew   = [];
        $thisyearexpectedDataNew = [];
        foreach ($thisMonthhDate as $val) {
            if (isset($thisyearactualData[$val])) {
                $thisyearactualDataNew[] = $thisyearactualData[$val];
            } else {
                $thisyearactualDataNew[] = 0;
            }
            if (isset($thisyearexpectedData[$val])) {
                $thisyearexpectedDataNew[] = $thisyearexpectedData[$val];
            } else {
                $thisyearexpectedDataNew[] = 0;
            }
        }
        $data['thisyear']['datetime']             = $thisMonthhDate;//今年收入
        $data['thisyear']['data']['actualData']   = $thisyearactualDataNew;//预估收入
        $data['thisyear']['data']['expectedData'] = $thisyearexpectedDataNew;//结算收入

        $lastyear               = ToolsController::timeChange(13);
        $selfdobusinessdatetime = [
            '2019-01' => '2019-01',
            '2019-02' => '2019-02',
            '2019-03' => '2019-03',
            '2019-04' => '2019-04',
            '2019-05' => '2019-05',
            '2019-06' => '2019-06',
            '2019-07' => '2019-07',
            '2019-08' => '2019-08',
            '2019-09' => '2019-09',
            '2019-10' => '2019-10',
            '2019-11' => '2019-11',
            '2019-12' => '2019-12',
        ];

        $selfdobusinessactualData   = $order->orderStatisticsMonth($lastyear, '1,2,4,6');
        $selfdobusinessexpectedData = $order->orderStatisticsMonth($lastyear, '4,6');

        $selfdobusinessactualDataNew   = [];
        $selfdobusinessexpectedDataNew = [];
        foreach ($selfdobusinessdatetime as $val) {
            if (isset($selfdobusinessactualData[$val])) {
                $selfdobusinessactualDataNew[] = $selfdobusinessactualData[$val];
            } else {
                $selfdobusinessactualDataNew[] = 0;
            }
            if (isset($selfdobusinessexpectedData[$val])) {
                $selfdobusinessexpectedDataNew[] = $selfdobusinessexpectedData[$val];
            } else {
                $selfdobusinessexpectedDataNew[] = 0;
            }
        }
        $data['selfdobusiness']['datetime']             = $selfdobusinessdatetime;//去年收入
        $data['selfdobusiness']['data']['actualData']   = $selfdobusinessactualDataNew;//预估收入
        $data['selfdobusiness']['data']['expectedData'] = $selfdobusinessexpectedDataNew;//结算收入
        $this->ok($data);
    }

    public function dayInfoAction()
    {
        $day = isset($this->params['day']) ? $this->params['day'] : date('Y-m-d');
        list($today['start_at'], $today['end_at']) = array(date('Y-m-d H:i:s', strtotime($day)), date('Y-m-d H:i:s', strtotime($day) + 24 * 3600 - 1));
        list($lasttoday['start_at'], $lasttoday['end_at']) = array(date('Y-m-d H:i:s', strtotime('-1 day', strtotime($day))), date('Y-m-d H:i:s', strtotime('-1 day', strtotime($day) + 24 * 3600 - 1)));

        list($toweek['start_at'], $toweek['end_at']) = array(date('Y-m-d H:i:s', strtotime('-7 day', strtotime($day))), $today['start_at']);
        list($lastweek['start_at'], $lastweek['end_at']) = array(date('Y-m-d H:i:s', strtotime('-14 day', strtotime($day))), $toweek['start_at']);
        $order  = new Orders();
        $member = new Member();
        //会员数
        $data['members']['membersCount']      = $member->countNumber($today);//今日会员
        $data['members']['thedaybefore']      = $member->userCompare($today, $lasttoday);//较前一日
        $data['members']['thedaybefore_flag'] = $data['members']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['members']['lastweek']          = $member->userCompare($toweek, $lastweek);//较上周
        $data['members']['lastweek_flag']     = $data['members']['lastweek'] > 0 ? 1 : 0;//上周同期

        $data['order']['orderCount']        = $order->orderCount($today);//今日订单
        $data['order']['thedaybefore']      = $order->orderCompare($today, $lasttoday);//较前一日
        $data['order']['thedaybefore_flag'] = $data['order']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['order']['lastweek']          = $order->orderCompare($toweek, $lastweek);//较上周
        $data['order']['lastweek_flag']     = $data['order']['lastweek'] > 0 ? 1 : 0;;//上周同期

        $data['todayestimate']['thismonthCount']    = $order->orderCountCommition($today);//今日预估
        $data['todayestimate']['thedaybefore']      = $order->orderCompareCommition($today, $lasttoday);
        $data['todayestimate']['thedaybefore_flag'] = $data['todayestimate']['thedaybefore'] > 0 ? 1 : 0;//1上升0下降
        $data['todayestimate']['thismonth']         = $order->orderCompareCommition($toweek, $lastweek);
        $data['todayestimate']['lastweek_flag']     = $data['todayestimate']['thismonth'] > 0 ? 1 : 0;;//上周同期

        $data['selfdobusiness']['selfdobusinessCount'] = '10000';//自营订单
        $data['selfdobusiness']['thedaybefore']        = '0.17%';
        $data['selfdobusiness']['thedaybefore_flag']   = 1;//1上升0下降
        $data['selfdobusiness']['lastweek']            = '0.14%';
        $data['selfdobusiness']['lastweek_flag']       = 0;//上周同期

        $data['selfestimate']['thismonthCount']    = 1;//自营预估
        $data['selfestimate']['thedaybefore']      = 1;
        $data['selfestimate']['thedaybefore_flag'] = 0;//1上升0下降
        $data['selfestimate']['thismonth']         = 1;
        $data['selfestimate']['lastweek_flag']     = 1;//上周同期
        $this->ok($data);
    }

    public function sectionInfoAction()
    {
        $time['start_at'] = isset($this->params['start_at']) ? $this->params['start_at'] : '';
        $time['end_at']   = isset($this->params['end_at']) ? $this->params['end_at'] : '';

        $order                       = new Orders();
        $member                      = new Member();
        $data['membersCount']        = 0;
        $data['orderCount']          = 0;
        $data['thismonthCount']      = 0;
        $data['selfdobusinessCount'] = 0;
        $data['selfestimateCount']   = 0;
        if ($time['start_at'] != '' && $time['end_at'] != '') {
            $data['membersCount'] = $member->countNumber($time);//会员

            $data['orderCount'] = $order->orderCount($time);//订单

            $data['thismonthCount'] = $order->orderCountCommition($time);//预估

            $data['selfdobusinessCount'] = '10000';//自营订单

            $data['selfestimateCount'] = 1;//自营预估
        }
        $this->ok($data);
    }


    /**
     * 会员统计
     */
    public function index_memberAction()
    {
        if (!isset($this->params['start_at']) || empty($this->params['start_at']) || !isset($this->params['end_at']) || empty($this->params['end_at'])) {
            $datetime                 = CommonAd::timeChange(3);
            $this->params['start_at'] = date('Y-m-d', strtotime($datetime['start_at']));
            $this->params['end_at']   = date('Y-m-d', strtotime($datetime['end_at']));
        }
        $datamytime    = $this->getDateFromRange($this->params['start_at'], $this->params['end_at']);
        $memberDb      = new Member();
        $user_find_one = $memberDb->datetime_day($this->params);
        $user_count    = [];
        foreach ($datamytime as $key => $value) {
            $user_count[$key] = isset($user_find_one[$value]) ? $user_find_one[$value] : '0';
        }
        $result['count_all']   = $memberDb->count_member_all();
        $result['day']   = $datamytime;
        $result['count'] = $user_count;

        $upmonth        = CommonAd::timeChange(9);//上月
        $upmonth_result = $memberDb->count_member($upmonth);

        $thismonth                   = CommonAd::timeChange(8);//本月
        $thismonth_result['count']   = $memberDb->count_member($thismonth);
        $thismonth_result['persent'] = $thismonth_result['count'] - $upmonth_result != 0 ? CommonAd::number_randtwo((($thismonth_result['count'] - $upmonth_result) / $upmonth_result) * 100) : 0;
        $result['thismonth']         = $thismonth_result;


        $up_week        = CommonAd::timeChange(15);//上周
        $up_week_result = $memberDb->count_member($up_week);

        $this_week                   = CommonAd::timeChange(14);//本周
        $this_week_result['count']   = $memberDb->count_member($this_week);
        $this_week_result['persent'] = $this_week_result['count'] - $up_week_result != 0 ? CommonAd::number_randtwo((($this_week_result['count'] - $up_week_result) / $up_week_result) * 100) : 0;
        $result['this_week']         = $this_week_result;


        $yestoday        = CommonAd::timeChange(2);//昨日
        $yestoday_result = $memberDb->count_member($yestoday);

        $today                   = CommonAd::timeChange(1);//今日
        $today_result['count']   = $memberDb->count_member($today);
        $today_result['persent'] = $today_result['count'] - $yestoday_result != 0 ? CommonAd::number_randtwo((($today_result['count'] - $yestoday_result) / $yestoday_result) * 100) : 0;
        $result['today_result']  = $today_result;

        $this->ok($result);
    }


    //收益统计
    public function income_staticAction()
    {
        if (!isset($this->params['start_at']) || empty($this->params['start_at']) || !isset($this->params['end_at']) || empty($this->params['end_at'])) {
            $datetime                 = CommonAd::timeChange(3);
            $this->params['start_at'] = date('Y-m-d', strtotime($datetime['start_at']));
            $this->params['end_at']   = date('Y-m-d', strtotime($datetime['end_at']));
        }
        $datamytime = $this->getDateFromRange($this->params['start_at'], $this->params['end_at']);

        $orderCommissonDb = new Orders();

        $settle_all = $orderCommissonDb->settle_function_sum();
        $result['settle_all'] = CommonAd::number_randtwo($settle_all);
        $user_find_one    = $orderCommissonDb->datetime_day($this->params, '2,4,6');    //预估收益
        $estimate         = [];
        $result['day']    = $datamytime;
        foreach ($datamytime as $key => $value) {
            $estimate[$key] = isset($user_find_one[$value]) ? $user_find_one[$value] : '0';
        }
        $result['estimate'] = $estimate;

        $settle_arr = $orderCommissonDb->datetime_day($this->params, '4,6', 'tk_earning_time');    //结算收益
        $settle     = [];
        foreach ($datamytime as $key => $value) {
            $settle[$key] = isset($settle_arr[$value]) ? $settle_arr[$value] : '0';
        }
        $result['settle'] = $settle;


        $upmonth                 = CommonAd::timeChange(9);//上月
        $upmonth_estimate_result = $orderCommissonDb->estimate_function($upmonth);
        $upmonth_settle_result   = $orderCommissonDb->settle_function($upmonth);

        $thismonth                            = CommonAd::timeChange(8);//本月
        $thismonth_estimate_result            = $orderCommissonDb->estimate_function($thismonth);
        $thismonth_result['estimate_sum']     = $thismonth_estimate_result;
        $thismonth_result['estimate_persent'] = $thismonth_estimate_result - $upmonth_estimate_result != 0 ? CommonAd::number_randtwo((($thismonth_estimate_result - $upmonth_estimate_result) / $upmonth_estimate_result) * 100) : 0;
        $thismonth_settle_result              = $orderCommissonDb->settle_function($thismonth);
        $thismonth_result['settle_sum']       = $thismonth_settle_result;
        $thismonth_result['settle_persent']   = $thismonth_settle_result - $upmonth_settle_result != 0 ? CommonAd::number_randtwo((($thismonth_settle_result - $upmonth_settle_result) / $upmonth_settle_result) * 100) : 0;
        $result['thismonth']                  = $thismonth_result;

        $up_week                 = CommonAd::timeChange(15);//上周
        $up_week_estimate_result = $orderCommissonDb->estimate_function($up_week);
        $up_week_settle_result   = $orderCommissonDb->settle_function($up_week);

        $this_week                            = CommonAd::timeChange(14);//本周
        $this_week_estimate_sum               = $orderCommissonDb->estimate_function($this_week);
        $this_week_result['estimate_sum']     = $this_week_estimate_sum;
        $this_week_result['estimate_persent'] = $this_week_estimate_sum - $up_week_estimate_result != 0 ? CommonAd::number_randtwo((($this_week_estimate_sum - $up_week_estimate_result) / $up_week_estimate_result) * 100) : 0;
        $this_week_settle_sum                 = $orderCommissonDb->settle_function($this_week);
        $this_week_result['settle_sum']       = $this_week_settle_sum;
        $this_week_result['settle_persent']   = $this_week_settle_sum - $up_week_settle_result != 0 ? CommonAd::number_randtwo((($this_week_settle_sum - $up_week_settle_result) / $up_week_settle_result) * 100) : 0;
        $result['this_week']                  = $this_week_result;

        $yestoday                 = CommonAd::timeChange(2);//昨日
        $yestoday_estimate_result = $orderCommissonDb->estimate_function($yestoday);
        $yestoday_settle_result   = $orderCommissonDb->settle_function($yestoday);

        $today                            = CommonAd::timeChange(1);//今日
        $today_estimate_sum               = $orderCommissonDb->estimate_function($today);
        $today_result['estimate_sum']     = $today_estimate_sum;
        $today_result['estimate_persent'] = $today_estimate_sum - $yestoday_estimate_result != 0 ? CommonAd::number_randtwo((($today_estimate_sum - $yestoday_estimate_result) / $yestoday_estimate_result) * 100) : 0;
        $today_settle_sum                 = $orderCommissonDb->settle_function($today);
        $today_result['settle_sum']       = $today_settle_sum;
        $today_result['settle_persent']   = $today_settle_sum - $yestoday_settle_result != 0 ? CommonAd::number_randtwo((($today_settle_sum - $yestoday_settle_result) / $yestoday_settle_result) * 100) : 0;
        $result['today']                  = $today_result;
        $this->ok($result);
    }

}
